﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SeDuce.IDAL;
using System.Data.SqlClient;

namespace SeDuce.DAL
{
    public class AdDAL : IAdDAL
    {
        #region SQL
        public const string sqlGetAds = "SELECT * FROM TbAd";
        public const string sqlGetAdByID = "SELECT * FROM TbAd WHERE (AdID = {0})";
        const string sqlUpdateAd = "UPDATE TbAd SET AdName = {0}, Image = {1}, Enabled = {2}, Url={3} WHERE (AdID = {4})";
        const string sqlInsertAd = "INSERT INTO TbAd (AdName, Image, Enabled, Url) VALUES ({0},{1},{2},{3})";
        const string sqlDeleteAd = "Delete from TbAd where AdID={0}";
        #endregion

        #region Variables
        private static DataAccessObjectBase _dao = null;
        public static DataAccessObjectBase DAO
        {
            get
            {
                if (_dao == null)
                    _dao = new DataAccessObjectBase();
                return _dao;
            }
            set
            {
                _dao = value;
            }
        }
        #endregion

        public System.Data.DataTable GetAds()
        {
            return DAO.Select(sqlGetAds);
        }

        public System.Data.DataTable GetAdByID(int adID)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@AdID", adID)
            };
            string sql = string.Format(sqlGetAds, "@AdID");
            return DAO.Select(sql, sp);
        }

        public bool UpdateAd(Model.Ad ad)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@AdName", ad.AdName),
                new SqlParameter ("@Image", ad.Image),
                new SqlParameter ("@Enabled", ad.Enabled),
                new SqlParameter ("@Url", ad.Url),
                new SqlParameter ("@AdID", ad.AdID)
            };
            string sql = string.Format(sqlUpdateAd, "@AdName", "@Image", "@Enabled", "@Url", "@AdID");
            return DAO.ExecuteSql(sql, sp);
        }

        public bool InsertAd(Model.Ad ad)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@AdName", ad.AdName),
                new SqlParameter ("@Image", ad.Image),
                new SqlParameter ("@Enabled", ad.Enabled),
                new SqlParameter ("@Url", ad.Url)
            };
            string sql = string.Format(sqlInsertAd, "@AdName", "@Image", "@Enabled", "@Url");

            return DAO.ExecuteSql(sql, sp);
        }

        public bool DeleteAd(int adID)
        {
            SqlParameter[] sp = new SqlParameter[] { 
                new SqlParameter ("@AdID",adID),
            };
            string sql = string.Format(sqlDeleteAd, "@AdID");
            return DAO.ExecuteSql(sql, sp);
        }
    }
}
